package ibmetp.mis.controller.user;

import ibmetp.mis.common.Constants;
import ibmetp.mis.service.UserService;
import ibmetp.mis.serviceImpl.UserServiceImpl;
import ibmetp.mis.vo.Dept;
import ibmetp.mis.vo.Page;
import ibmetp.mis.vo.User;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

/** 
 * 某个角色的用户
 * @author LAN DABIN
 * /user/roleuser.json
 */
public class UserRolePageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserRolePageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 request.setCharacterEncoding(Constants.ENCODING_UTF8);
			
			//当前页号
			int pageNo=request.getParameter("page")!=null?Integer.parseInt(request.getParameter("page")):1;
			System.out.println("pageNo:"+pageNo);
			
			//页面大小
			int pageSize=request.getParameter("rows")!=null?Integer.parseInt(request.getParameter("rows")):2;
			System.out.println("pageSize:"+pageSize);
			//角色Id
			Long roleId=Long.valueOf(request.getParameter("roleId"));
			//查询条件User
			User user=new User();
			user.setUserName(request.getParameter("userName"));
			user.setUserNo(request.getParameter("userNo"));
			
			if(request.getParameter("deptId")!=null){
				Dept dept=new Dept();
				dept.setDeptId(Long.valueOf(request.getParameter("deptId")));
				user.setDept(dept);
				System.out.println(dept.getDeptId());
			}
			
			//System.out.println("SearchConditions:"+user);
			
			//查询数据
			UserService us=new UserServiceImpl();
		
			Page p=us.getRoleUsers(roleId, pageNo, pageSize, user);	   
			
			//输出JSON数据
			JSONObject o=new JSONObject();
			o.put("rows",p.getList());
			o.put("total",p.getTotal());
			response.setCharacterEncoding(Constants.ENCODING_UTF8);
			response.getWriter().write(o.toString());
	}

}
